﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web_HengliSeed.Sys_Manage
{
    public partial class M_EditProductInfo : System.Web.UI.Page
    {
        protected string ProImage = "../Images/noImage.gif";
        protected void Page_Load(object sender, EventArgs e)
        {
            ClassFile.Function.CheckState();
            //if (!string.IsNullOrEmpty(Request["lan"]))
            //{
            //    hfLan.Value = Request["lan"];
            //}
            if (!IsPostBack)
            {
                LoadData();
                BindDDL();
            }
        }

        private void BindDDL()
        {
            string lan = "cn";
            if (!String.IsNullOrEmpty(Request["lan"]))
                lan = Request["lan"];

            DDLProClass.DataSource = BLL.ProductInfo.GetClassMesAll(lan);
            DDLProClass.DataTextField = "T_ProClassName";
            DDLProClass.DataValueField = "T_ProClassID";
            DDLProClass.DataBind();
        }

        private void LoadData()
        {
            string strPID = Common.GetMes.GetRequestQuery(Request, "PID");
            if (!string.IsNullOrEmpty(strPID))
            {
                OleDbDataReader DataRead = BLL.ProductInfo.GetProInfo(strPID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtProName.Value = Server.HtmlDecode(DataRead["T_ProductName"].ToString());
                    txtProNote.Value = Server.HtmlDecode(DataRead["T_ProductNote"].ToString());
                    ProImage = "../UpLoad/Image/Small/" + DataRead["T_ProductImage"].ToString();
                    DDLProClass.SelectedValue = DataRead["T_ProClassID"].ToString();
                    if (!string.IsNullOrEmpty(DataRead["T_VideoPath"].ToString()))
                    {
                        hfVPath.Value = DataRead["T_VideoPath"].ToString();
                        uploadPanel.Visible = false;
                        videoPanel.Visible = true;
                        lblVPath.Text = hfVPath.Value;
                    }
                    
                    RBProBase.Checked = (DataRead["T_ProClassID"].ToString() == "0") ? true : false;
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string strPID = Common.GetMes.GetRequestQuery(Request, "PID");
            int ProClass = (RBProBase.Checked) ? 0 : 1;
            string strProName = Server.HtmlEncode(txtProName.Value.Trim());

            string lan = "cn";
            if (!String.IsNullOrEmpty(Request["lan"]))
                lan = Request["lan"];

            if (BLL.ProductInfo.EditProductMes(strPID, strProName, DDLProClass.SelectedValue, HImageName.Value, ProClass, Server.HtmlEncode(txtProNote.Value.Trim()), lan,hfVPath.Value.Trim()))
            {
                if (!string.IsNullOrEmpty(strPID))
                {
                    Common.ShowMessage.Show(Page, "error", "信息保存成功...", "M_ProductList.aspx?lan=" + lan);
                }
                else
                {
                    Common.ShowMessage.myScriptMes(Page, "Suess", "if(confirm(\"信息保存成功，是否继续添加？否则返回信息列表。\")) {location.href=\"M_EditProductInfo.aspx?lan=" + lan + "\"} else  {location.href=\"M_ProductList.aspx?lan=" + lan + "\"} ");
                }
            }
            else
            {
                Common.ShowMessage.Show(Page, "error", "信息保存失败， 请重试或联系管理人员...");
            }
        }

        protected void BtnUpLoad_Click(object sender, EventArgs e)
        {
            HImageName.Value = (HImageName.Value == "noImage.gif") ? Common.GetMes.GetNowTimeString() + UploadImg.FileName.Substring(UploadImg.FileName.LastIndexOf(".")) : HImageName.Value;
            switch (ClassFile.Function.UpFile(Page, UploadImg, "img", HImageName.Value))
            {
                case 1:
                    string newsImageName = HImageName.Value;
                    string smallImageName = HImageName.Value.Substring(0, HImageName.Value.IndexOf("."));
                    string strBakName = HImageName.Value.Substring(HImageName.Value.IndexOf(".") + 1);//5+1+a+s+p+x
                    ClassFile.Function.MakeThumbnail(Server.MapPath("~/UpLoad/image/" + newsImageName), Server.MapPath("~/UpLoad/image/Small/" + smallImageName + "." + strBakName), 130, 150, "Cut", strBakName);
                    ProImage = "../UpLoad/image/" + newsImageName;
                    Common.ShowMessage.Show(Page, "e1", "上传完成");
                    break;
                case -1:
                    Common.ShowMessage.Show(Page, "e1", "上传文件格式有误，限制使用gif，jpeg，png。");
                    break;
                case -2:
                    Common.ShowMessage.Show(Page, "e2", "上传文件太大，请确保图片小于500K。");
                    break;
                default:
                    Common.ShowMessage.Show(Page, "e2", "未知错误，请重试或联系管理人员。");
                    break;
            }
        }

        protected void BtnUpLoadVideo_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UploadVideo.FileName))
            {
                return;
            }
            hfVPath.Value = Common.GetMes.GetNowTimeString() + UploadVideo.FileName.Substring(UploadVideo.FileName.LastIndexOf("."));
            switch (ClassFile.Function.UpFile(Page, UploadVideo, "video", hfVPath.Value))
            {
                case 1:
                    string newsImageName = hfVPath.Value;
                    string smallImageName = hfVPath.Value.Substring(0, hfVPath.Value.IndexOf("."));
                    string strBakName = hfVPath.Value.Substring(hfVPath.Value.IndexOf(".") + 1);
                    Common.ShowMessage.Show(Page, "e1", "上传完成");
                    uploadPanel.Visible = false;
                        videoPanel.Visible = true;
                        lblVPath.Text = hfVPath.Value;
                    //ClassFile.Function.MakeThumbnail(Server.MapPath("~/UpLoad/image/" + newsImageName), Server.MapPath("~/UpLoad/image/Small/" + smallImageName + "." + strBakName), 130, 150, "Cut", strBakName);
                    //ProImage = "../UpLoad/image/" + newsImageName;
                    break;
                case -1:
                    Common.ShowMessage.Show(Page, "e1", "上传文件格式有误，限制使用avi。");
                    break;
                case -2:
                    Common.ShowMessage.Show(Page, "e2", "上传文件太大。");
                    break;
                default:
                    Common.ShowMessage.Show(Page, "e2", "未知错误，请重试或联系管理人员。");
                    break;
            }
        }

        protected void btnClearVideo_Click(object sender, EventArgs e)
        {
            hfVPath.Value = string.Empty;
            lblVPath.Text = string.Empty;
            uploadPanel.Visible = true;
            videoPanel.Visible = false;
        }
    }
}
